import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import classification_report
from joblib import dump

from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
from joblib import dump

class ClassIfication(object):
    def __init__(self):
        self.df=pd.read_csv('daily.csv')

    def get_condition(self):
        """
        分类
        """

        df=self.df
        df['max_ratio']-df['max_close"] /df[the_close'] #收m潜力
        df['mim_ratio']-df['nin_close']/df['the_close']#风检程度
                #自动划分高低阅值
        high_return_threshold=df['max_ratio'].quantile(0.4) #前4e定义为高收益
        high_risk_threshold=df['mim_ratio'].quantile(0.4) #前4ox定义为高风验
                                                        # 生成分类标签
        conditions=[
            (df['max_ratio']>=high_return_threshold)&(df['mim_ratio']<=high_risk_threshold),
            (df["max_ratio"] <=high_return_threshold) &(df['mim_ratio']>high_risk_threshold),
            (df['maxratio']<high_return_threshold)& (df['min_ratio']>high_risk_threshold),
            (df['naxratio']<high_return_threshold) & (df['mim_ratio'] <=high_risk_threshold),
            ]
        labels=["高收益高风险","高收益低风险","低收益低风险","低收益高风险"]
        df['category']=np.select(conditions, labels, default='末知')


        
        features=df[[ 
              'eps','total revenue_ps','undist profit_ps', 'gross_margin', 'fcff', 'fcfe', 'tangible_asset', "bps", 'grossprofit_margin', 'npta'
        ]]

        from sklearn.preprocessing import LabelEncoder
        le=LabelEncoder
        df['catrgory_encoded']=le.fit_transform(["catrgory"])


        #dump(scaler,)
        scaler =StandardScaler()
        x=scaler.fit_transform(features)
        y=df['catrgory_encoded']

        X_train,X_test,y_train,y_test =train_test_split(X,y,test_size=0.3,random_state=24)
        return X_train,X_test,y_train,y_test,le


    def knn_utils(self,X_train,X_test,y_train,y_test,le):
        """
        knn模型
        """    

        knn=KNeighborsClassifier(n_neighbors=3)
        knn.fit(X_train,y_train)

        y_pred=knn.predict(X_test)
        print(classification_report(y_test,y_pred,target_names=le.classes_))

        dump(knn,'knn_classIfien.joblib')

    def svc_utils(self,X_train,X_test,y_train,y_test,le):
        """
        向量机方法
        """

        svc=SVC()
        svc.fit(X_train,y_train)
        predict = svc.predict(x_test)
        print("accuracy_score:%.4lf" % accuracy_score(predict,y_test))
        print("Classification report for classifier %s: \n %s\n" %(svc,classification_report(y_test,predict)))

if __name__ =='__mian__':
    ci=ClassIfication()
    X_train,X_test,y_train,y_test=ci.get_condition()
    ci.knn_utils(X_train,X_test,y_train,y_test,le)
     # 调用 knn_utils 方法
    #ci.svc_utils(x_train, x_test, y_train, y_test)